package com.molinari.coding.dati;

import java.sql.Connection;
import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.molinari.androidstructure.data.db.ConnectionPool;
import com.molinari.coding.R;
import com.molinari.jdbcandroid.wrap.ConnectionAndroid;

public class ConnectionPoolCoding extends ConnectionPool {

	
	private static ConnectionPool singleton;
	
	public ConnectionPoolCoding(Context context) {
		super(context);
	}
	public static synchronized ConnectionPool getSingleton(Context context) {
		if(singleton == null){
			try {
				singleton = new ConnectionPoolCoding(context);
			}
			catch (Exception e) {
				Log.e(ConnAndroidCharge.class.getName(), Log.getStackTraceString(e));
			}
		}
		return singleton;
	}
	@Override
	protected Connection newConnection() {
		return new ConnAndroidCharge(applicationContext, true);
	}

	@Override
	protected int getNumeroConnessioniDisponibili() {
		return 50;
	}
	
	@Override
	public void chiudiOggettiDb(Connection cn) throws SQLException {
		super.chiudiOggettiDb(cn);
		if(cn != null){
			if(cn instanceof ConnectionAndroid){
				((ConnectionAndroid)cn).getDb().close();
			}
		}
		
	}
	
	private class ConnAndroidCharge extends ConnectionAndroid{

		public ConnAndroidCharge(Context context, boolean isWritable) {
			super(context, isWritable);
		}
		public ConnAndroidCharge(final Context context, final SQLiteDatabase db) {
			super(context, db);
		}

		@Override
		public Class<?> getDatabaseClass() {
			return DatabaseAndroidCoding.class;
		}
	}
}
